/*
 * Copyright (c) 2018.
 * 北京九思金信科技有限公司对该文件内容保留所有权利。
 * 未经许可，不得私自拷贝、传播、发布、引用该文件内容，违者将被追究法律责任。
 */

import Vue from "vue";
import Router from "vue-router";
import Login from "@/views/login/Login.vue";
import Nav from "@/components/JoysHeader/JoysHeader.vue";

import Home from "@/views/nav/home/Home.vue";
import Info from "@/views/nav/info/Info.vue";
import Sue from "@/views/nav/sue/Sue.vue";
import My from "@/views/nav/my/My.vue";

// // home
import HomeIndex from "@/views/nav/home/component/HomeIndex.vue";
import Task from "@/views/nav/home/component/Task.vue";
import TaskItem from "@/views/nav/home/component/TaskItem.vue";

// Info
import InfoIndex from "@/views/nav/info/component/InfoIndex.vue";
import InfoList from "@/views/nav/info/component/InfoList.vue";
import InfoSearch from "@/views/nav/info/component/InfoSearch.vue";
import InfoTask from "@/views/nav/info/component/InfoTask.vue";

// my
import MyIndex from "@/views/nav/my/component/MyIndex.vue";
import MySet from "@/views/nav/my/component/MySet.vue";
import MyRegards from "@/views/nav/my/component/MyRegards.vue";
import MySolve from "@/views/nav/my/component/MySolve.vue";

// sue
import SueIndex from "@/views/nav/sue/component/SueIndex.vue";
import SueSearch from "@/views/nav/sue/component/SueSearch.vue";

Router.prototype.goBack = function() {
  this.isBack = true;
  window.history.go(-1);
};

Vue.use(Router);

const router = new Router({
  mode: "history",
  routes: [
    { path: "/", name: "login", component: Login },
    { path: "*", component: Login },
    { path: "/nav", name: "nav", component: Nav, redirect: "/Home" },
    // {path: "sueSearch", name: "sueSearch", component: SueSearch},

    {
      path: "/home",
      name: "home",
      component: Home,
      children: [
        { path: "", name: "homeIndex", component: HomeIndex },
        { path: "task", name: "task", component: Task },
        { path: ":id", name: "taskItem", component: TaskItem }
      ]
    },
    {
      path: "/info",
      name: "info",
      component: Info,
      children: [
        { path: "", name: "infoIndex", component: InfoIndex },
        { path: "infoSearch", name: "infoSearch", component: InfoSearch },
        { path: ":id", name: "infoList", component: InfoList }
      ]
    },
    {
      path: "/sue",
      name: "sue",
      component: Sue,
      children: [
        { path: "", name: "sueIndex", component: SueIndex },
        { path: "sueSearch", name: "sueSearch", component: SueSearch }
      ]
    },
    {
      path: "/my",
      name: "my",
      component: My,
      children: [
        { path: "", name: "myIndex", component: MyIndex },
        { path: "mySet", name: "mySet", component: MySet },
        { path: "myRegards", name: "myRegards", component: MyRegards },
        { path: "mySolve", name: "mySolve", component: MySolve }
      ]
    }
  ],
  scrollBehavior(to, from, savedPosition) {
    return { x: 0, y: 0 };
  }
});

// 配置路由的拦截器
router.beforeEach((to, from, next) => {
  // 如果访问登录的路由地址，放过
  if (to.name === "login") {
    next();
  } else {
    // 如果请求的不是登录页面，验证token
    //  获取本地存储中的token
    const token = localStorage.getItem("AUTH-TOKEN");
    if (!token) {
      // 如果没有token，跳转到登录
      next({
        name: "login"
      });
    } else {
      // 如果有token，继续往下执行
      next();
    }
  }
});

export default router;
